"""
看了官方的题解。。
"""


class Solution:
    def removeKdigits(self, num: str, k: int) -> str:
        stack = []
        for digit in num:
            while k and stack and stack[-1] > digit:
                stack.pop()
                k -= 1
            stack.append(digit)

        stack = stack[:-k] if k else stack
        print(stack, k)
        return "".join(stack).lstrip("0") or "0"


if __name__ == "__main__":

    # res = Solution().removeKdigits("1432219", 3)
    res = Solution().removeKdigits("123456", 3)
    # res = Solution().removeKdigits("10200", 1)
    print(res)
